package com.edu.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.edu.bean.Admin;
import com.edu.dao.AdminDao;
import com.edu.dao.BaseDao;
import java.sql.ResultSet;

public class AdminDaoImpl implements AdminDao {

	@Override
	public Admin selectAdmin(String adminName, String password) {
		Admin admin=null;
		String sql="SELECT * FROM  admin where account =? and password = ?";
		Connection con =null;
		PreparedStatement ps = null;
		ResultSet res=null;
		
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, adminName);
			ps.setString(2, password);
			res = ps.executeQuery();
			if(res.next()) {
				admin = new Admin();
				admin.setAdminId(res.getInt("adminId"));
				admin.setAdminName(res.getString("adminName"));
				admin.setPassword(res.getString("password"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(res, ps, con);
		}
		return admin;
	}

	@Override
	public Admin selectByName(String adminName) {
		Admin admin=null;
		String sql="SELECT * FROM  admin where account =?";
		Connection con =null;
		PreparedStatement ps = null;
		ResultSet res=null;
		
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			System.out.println(sql);
			ps.setString(1, adminName);
			res = ps.executeQuery();
			if(res.next()) {
				admin = new Admin();
				admin.setAdminId(res.getInt("adminId"));
				admin.setAdminName(res.getString("adminName"));
				admin.setPassword(res.getString("password"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(res, ps, con);
		}
		return admin;
	}

}
